{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    月考1  月考2  月考3  月考4\n",
      "语文   96   92   83   94\n",
      "数学   85   86   77   88\n",
      "英语   69   90   91   82\n"
     ]
    }
   ],
   "source": [
    "# 导入 Pandas 库\n",
    "import pandas as pd\n",
    "# 构造数据集\n",
    "df_data=pd.DataFrame([[96,92,83,94],[85,86,77,88],[69,90,91,82]],\n",
    "                 index=['语文','数学','英语'],\n",
    "                 columns=['月考1','月考2','月考3','月考4'])\n",
    "print(df_data)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "语文  月考1    96\n",
      "    月考2    92\n",
      "    月考3    83\n",
      "    月考4    94\n",
      "数学  月考1    85\n",
      "    月考2    86\n",
      "    月考3    77\n",
      "    月考4    88\n",
      "英语  月考1    69\n",
      "    月考2    90\n",
      "    月考3    91\n",
      "    月考4    82\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "# df_data 为上述构建的数据集\n",
    "print(df_data.stack())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     年度   学期  语文  数学\n",
      "a  2018  上学期  83  94\n",
      "b  2018  下学期  77  88\n",
      "c  2019  上学期  83  94\n",
      "d  2019  下学期  83  94\n",
      "e  2020  上学期  83  94\n",
      "f  2020  下学期  91  82\n"
     ]
    }
   ],
   "source": [
    "# 这里模拟了3年某位学生各学期语文、数学得分的数据表。\n",
    "df_data_pivot=pd.DataFrame([[\"2018\",\"上学期\",83,94],[\"2018\",\"下学期\",77,88],\n",
    "                            [\"2019\",\"上学期\",83,94],[\"2019\",\"下学期\",83,94],\n",
    "                            [\"2020\",\"上学期\",83,94],[\"2020\",\"下学期\",91,82]],\n",
    "                 index=['a','b','c','d','e','f'],\n",
    "                 columns=['年度','学期','语文','数学'])\n",
    "print(df_data_pivot)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "学期    上学期  下学期\n",
      "年度            \n",
      "2018   83   77\n",
      "2019   83   83\n",
      "2020   83   91\n"
     ]
    }
   ],
   "source": [
    "# pivot(index=\"年度\", columns=\"学期\", values=\"语文\") 透视表\n",
    "new_df=df_data_pivot.pivot(index=\"年度\", columns=\"学期\", values=\"语文\")\n",
    "print(new_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   编程语言 技术方向   推出时间  年均销售数量    价格                       主要创始人\n",
      "0  java   后端  1995年     230  45.6                JamesGosling\n",
      "1  HTML   前端  1990年     124  55.3           Daniel W Connolly\n",
      "2     C   后端  1972年      35  33.9  Dennis MacAlistair Ritchie\n",
      "3    js   前端  1995年     678  59.5                Brendan Eich\n",
      "4   C++   后端  1983年     125  75.0           Bjarne Stroustrup\n",
      "5   CSS   前端  1990年     254  24.6             Tim Berners-Lee\n"
     ]
    }
   ],
   "source": [
    "data_path_groupby=\"groupby.txt\"\n",
    "# 解析数据\n",
    "data = pd.read_csv(data_path_groupby,sep=\",\",engine=\"python\")\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>编程语言</th>\n",
       "      <th>技术方向</th>\n",
       "      <th>推出时间</th>\n",
       "      <th>年均销售数量</th>\n",
       "      <th>价格</th>\n",
       "      <th>主要创始人</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>java</td>\n",
       "      <td>后端</td>\n",
       "      <td>1995年</td>\n",
       "      <td>230</td>\n",
       "      <td>45.6</td>\n",
       "      <td>JamesGosling</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>C</td>\n",
       "      <td>后端</td>\n",
       "      <td>1972年</td>\n",
       "      <td>35</td>\n",
       "      <td>33.9</td>\n",
       "      <td>Dennis MacAlistair Ritchie</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>C++</td>\n",
       "      <td>后端</td>\n",
       "      <td>1983年</td>\n",
       "      <td>125</td>\n",
       "      <td>75.0</td>\n",
       "      <td>Bjarne Stroustrup</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   编程语言 技术方向   推出时间  年均销售数量    价格                       主要创始人\n",
       "0  java   后端  1995年     230  45.6                JamesGosling\n",
       "2     C   后端  1972年      35  33.9  Dennis MacAlistair Ritchie\n",
       "4   C++   后端  1983年     125  75.0           Bjarne Stroustrup"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 这里依据技术方向列进行分组\n",
    "data.groupby('技术方向').get_group('后端')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\ldc\\AppData\\Local\\Temp\\ipykernel_2088\\2376285757.py:1: FutureWarning: The default value of numeric_only in DataFrameGroupBy.sum is deprecated. In a future version, numeric_only will default to False. Either specify numeric_only or select only columns which should be valid for the function.\n",
      "  data.groupby(['技术方向','推出时间'],as_index=False).sum()\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>技术方向</th>\n",
       "      <th>推出时间</th>\n",
       "      <th>年均销售数量</th>\n",
       "      <th>价格</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>前端</td>\n",
       "      <td>1990年</td>\n",
       "      <td>378</td>\n",
       "      <td>79.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>前端</td>\n",
       "      <td>1995年</td>\n",
       "      <td>678</td>\n",
       "      <td>59.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>后端</td>\n",
       "      <td>1972年</td>\n",
       "      <td>35</td>\n",
       "      <td>33.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>后端</td>\n",
       "      <td>1983年</td>\n",
       "      <td>125</td>\n",
       "      <td>75.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>后端</td>\n",
       "      <td>1995年</td>\n",
       "      <td>230</td>\n",
       "      <td>45.6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  技术方向   推出时间  年均销售数量    价格\n",
       "0   前端  1990年     378  79.9\n",
       "1   前端  1995年     678  59.5\n",
       "2   后端  1972年      35  33.9\n",
       "3   后端  1983年     125  75.0\n",
       "4   后端  1995年     230  45.6"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.groupby(['技术方向','推出时间'],as_index=False).sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\ldc\\AppData\\Local\\Temp\\ipykernel_2088\\2405322123.py:1: FutureWarning: The default value of numeric_only in DataFrameGroupBy.sum is deprecated. In a future version, numeric_only will default to False. Either specify numeric_only or select only columns which should be valid for the function.\n",
      "  data.groupby(['技术方向','推出时间']).sum()\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>年均销售数量</th>\n",
       "      <th>价格</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>技术方向</th>\n",
       "      <th>推出时间</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">前端</th>\n",
       "      <th>1990年</th>\n",
       "      <td>378</td>\n",
       "      <td>79.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995年</th>\n",
       "      <td>678</td>\n",
       "      <td>59.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">后端</th>\n",
       "      <th>1972年</th>\n",
       "      <td>35</td>\n",
       "      <td>33.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1983年</th>\n",
       "      <td>125</td>\n",
       "      <td>75.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995年</th>\n",
       "      <td>230</td>\n",
       "      <td>45.6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            年均销售数量    价格\n",
       "技术方向 推出时间               \n",
       "前端   1990年     378  79.9\n",
       "     1995年     678  59.5\n",
       "后端   1972年      35  33.9\n",
       "     1983年     125  75.0\n",
       "     1995年     230  45.6"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.groupby(['技术方向','推出时间']).sum()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.5"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "96169d20a00e6f8b1336899eac0ac63ba1d744560862b1f14ce92f02632fdcb0"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
